import { observable, action, autorun,toJS } from 'mobx';
import {WeaTableNew,WeaForm} from 'comsMobx'
const {TableStore} = WeaTableNew;
import { ListStore } from './listStore';
import {WeaTools} from "ecCom"
import {Modal,message} from "antd"
import * as Apis from "../apis/instock"
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

class InstockCheckStore  extends ListStore{	
    title = "入库验收";
    topTab = [
		{ title: '入库验收', key: "0" },
		{ title: '历史验收记录', key: "1" },
    ];//已切换至页面
    @observable searchParams ={
        tabkey : "0"
    }
    @observable showSearchAd = false;
    @observable checkTableStore = new TableStore();
    conditiontype = {searchtype : "check"};

    @observable visible = false;
    @observable showDetail = false;
    @observable mainInstockInfo = [];  //主表
    @observable subInstockInfo = {};  //明细表
    @observable mainForm = new WeaForm();
    @observable rightMenu = [];
    instockid = "";
    subInfoData = [];
    @observable instockRemind;


    //历史记录
    @observable condition = [];
    @observable verified = false;
    @observable hasRight = false; //权限
    @observable ismanager = false; 
    

    @action
    initInstockCheckList=(params={})=>{   //入库验收列表
        this.title = this.title;
        this.loading = true;
        Apis.getInstockchecklist(params).then(data=>{
            if(data.isright == "false"  && typeof(data.isright) !== "undefined"){
				this.hasRight = false;
				this.verified = true;
			}else{
                this.hasRight = true;
				this.verified = true;
                this.checkTableStore.getDatas(data.sessionkey,  params.current || 1);
            }
            this.loading = false;
        })
    }
    @action 
    changeTab = (key)=>{
        this.searchParams={...this.searchParams,tabkey:key};
        if(key == "1"){
            this.doSearch();
            this.initDatas();
        }
    }

    initDatas=(params = {})=> {
		let newParams = {...this.conditiontype,...params};
		Apis.getInstockCondition(newParams).then(data=>{
            this.condition = data.condition;
            //根据高级搜索条件初始化form
			this.form && !this.form.isFormInit && this.form.initFormFields(data.condition);
		});
        
    }
    //获取历史记录
    doSearch =(params={})=>{
        this.loading = true;
         //获取表单的参数值
		 const searchParamsAd = this.form.getFormParams();
         const newParams = {  ...searchParamsAd, ...params };
         for(let item in newParams){
            if(newParams[item] == ""){
                delete newParams[item]
            }
         }
        Apis.getInstockcheckhislist(newParams).then(data=>{
            this.tableStore.getDatas(data.sessionkey,  params.current || 1);
            this.dataKey = data.sessionkey;
            this.ismanager = data.ismanager;
            this.loading = false;
        })
    }
    //显示定制列
    showColumn=()=>{
        if(this.searchParams.tabkey == "1"){
            this.tableStore.setColSetVisible(true);
            this.tableStore.tableColSet(true)
        }else{
            this.checkTableStore.setColSetVisible(true);
            this.checkTableStore.tableColSet(true)
        }
    }
    //作废
    doDeleteInstock = (params={}) =>{
        Apis.instockDelete(params).then(data=>{
            message.success(getLabel(83691,'作废成功！'));
            this.initInstockCheckList();
			//入库验收左侧菜单统计
			loadLeftMenuCount();
        })
    }
    //删除
    doDelete = (params={})=>{
        let _this = this;
        Apis.doDelete(params).then(data=>{
            if(data.flag){
                message.success(getLabel(91,"删除")+getLabel(15242,"成功"));
                _this.reLoad();
            }else{
                Modal.warning({
                    title: getLabel(15172, '系统提示'),
                    content: data.msg,
                }); 
            }
         })
    }

    //入库明细
    showInstockCheckInfo =(bool,params={},type)=>{
        this.showDetail = bool ;
        if(bool){
            this.mainForm = new WeaForm();
            this.mainInstockInfo = [];
            this.subInstockInfo = {};
            this.subInfoData = [];
            this.instockid = params.instockid ||"";
            if(type == "0"){
                this.rightMenu = [{
                    "isTop": "1",
                    "menuFun": "",
                    "menuIcon": "icon-coms-Reopen",
                    "menuName": getLabel(33036,"确认验收"),
                    "type": "BTN_CHECK"
                }];
            }else {
                this.rightMenu = []
            }
            Apis.getInstockcheckInfo(params).then(data=>{
                this.mainInstockInfo = data.mainTableInfo;
                this.subInstockInfo = data.subTableInfo ;
                this.subInfoData = data.subTableInfo.columnDefine || [];
                this.mainForm && !this.mainForm.isFormInit && this.mainForm.initFormFields(data.mainTableInfo);
            })
        }
    }
    //
    saveSubInfoData = (data)=>{
        this.subInfoData = data;
    }
    //验收
    doCheckInstock=(params={})=>{
        this.visible = true;
        this.mainForm.validateForm().then(f =>{
            if(f.isValid&&this.instockRemind.showErrors()){
                const searchParamsAd = this.mainForm.getFormParams();
                const newParams = { instockid:this.instockid, ...searchParamsAd, ...params };
                let par = {...newParams};
                par.subfields = JSON.stringify(this.subInfoData);
                if(newParams.cptdept_to){
                    Apis.doInstockcheckCode(par).then(data=>{
                        if(data.flag){
                            this.showInstockCheckInfo(false);
                            Apis.docheckinstock(par).then(data=>{
                                message.success(getLabel(33110,'验收')+getLabel(84565,'成功！'));
                                this.initInstockCheckList();
                                this.subInfoData = [];
                                    //入库验收左侧菜单统计
                                loadLeftMenuCount();
                            })
                        }else{
                            Modal.warning({
                                title: getLabel(15172, '系统提示'),
                                content: data.msg,
                            });
                        }
                        this.visible = false;
                    })
                }else{
                    f.showErrors();
                    this.visible = false;
                }
            }else{
                this.instockRemind.showErrors();
                f.showErrors();
                this.visible = false;
            }
        });
    }

  

	clearStatus=()=>{
        this.searchParams = {tabkey:"0"};
        this.showSearchAd = false;
        this.mainInstockInfo = [];
        this.subInstockInfo = {};
        this.subInfoData = [];
        this.clearFormFields();
    }

}

const cptInstockCheckStore = new InstockCheckStore();
export default cptInstockCheckStore;